package com.study.service1.controller;

import com.alibaba.csp.sentinel.Entry;
import com.alibaba.csp.sentinel.SphU;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;


@RestController
@RequestMapping("/jsontom")
@RefreshScope//动态刷新配置
public class HelloController {

    @Value("${student.name}")
    private String name;
    @Value("${server.port}")
    private String port;

    @GetMapping("/hello")
    public String hello(){
        return "Hello, "+name+" my Lord...."+port;
    }

    @GetMapping("/hi")
    public String hi(){
        return "Hi,"+name+" my Lord...."+port;
    }

    @PostMapping("/hi1/{username}")
    public String hi1(@PathVariable("username") String username){
        return "Hi, "+username+" my Lord....";
    }

    @GetMapping("/doSome")
    public String doSome(){

        return doSomething();
    }

    // 被流控的方法
    public static String doSomething(){
        try (Entry entry = SphU.entry("doSomething")){
            // 业务逻辑处理
            return "Hi," + " Do you want do something ?...."  + "  "+System.currentTimeMillis();
        }catch (BlockException e){
            // 被流控的逻辑
            System.out.println("触发流控！");
            return "嗨," + " 当前访问人数过多，请稍后再试！...."  + "  "+System.currentTimeMillis();

        }
    }

}
